package com.filex.ads.common.utils;

import java.util.concurrent.atomic.AtomicLong;

public class Utils {

    private static final AtomicLong sNextGeneratedId = new AtomicLong(1);

//    public static String sha1(String string) {
//        StringBuilder stringBuilder = new StringBuilder();
//
//        try {
//            MessageDigest digest = MessageDigest.getInstance("SHA-1");
//            byte[] bytes = string.getBytes("UTF-8");
//            digest.update(bytes, 0, bytes.length);
//            bytes = digest.digest();
//
//            for (final byte b : bytes) {
//                stringBuilder.append(String.format("%02X", b));
//            }
//
//            return stringBuilder.toString().toLowerCase(Locale.US);
//        } catch (Exception e) {
//            return "";
//        }
//    }

    public static long generateUniqueId() {
        for (;;) {
            final long result = sNextGeneratedId.get();
            long newValue = result + 1;
            if (newValue > Long.MAX_VALUE - 1) {
                newValue = 1;
            }
            if (sNextGeneratedId.compareAndSet(result, newValue)) {
                return result;
            }
        }
    }
}
